package com.zhanghao.sdk.presenter;

import android.os.Handler;
import android.os.Looper;

import com.zhanghao.sdk.callback.IZHServiceCallback;
import com.zhanghao.sdk.constant.ENUM_SERVICE;
import com.zhanghao.sdk.service.LoginServiceImpl;
import com.zhanghao.sdk.service.ServiceFactory;
import com.zhanghao.sdk.view.ILoadingView;
import com.zhanghao.sdk.view.ILoginView;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

public class LoginPresenter extends BasePresenter implements IZHServiceCallback {
    private LoginServiceImpl loginService;
    private ILoginView mLoginView;
    private ILoadingView mLoadingView;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public LoginPresenter(ILoginView loginView, ILoadingView loadingView) {
        this.mLoginView = loginView;
        this.mLoadingView = loadingView;
        this.loginService = (LoginServiceImpl) ServiceFactory.getInstance().getService(ENUM_SERVICE.LOGIN);
    }

    public void login() {
        mLoadingView.showLoading();
        loginService.accountLogin(mLoginView.getUsername(), mLoginView.getPassword(), LoginPresenter.this);
    }

    public void autoLogin() {
        mLoadingView.showLoading();
        loginService.tokenLogin(getToken(), LoginPresenter.this);
    }

    private int count = 0;
    public void sendMessage() {
        if (count != 0) {
            return;
        }
        mLoadingView.showLoading();
        loginService.sendMessage(mLoginView.getPhoneNumber(),mLoginView.getType(), LoginPresenter.this);
        final Timer timer = new Timer();
        final String oldTitle = mLoginView.getCodeBtnTitle();
        mLoginView.CodeBtnEnable(false);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        count = count - 1;
                        if (count > 0) {
                            mLoginView.setCodeBtnTitle(count + "秒");
                        } else {
                            mLoginView.setCodeBtnTitle(oldTitle);
                            mLoginView.CodeBtnEnable(true);
                            timer.cancel();
                        }
                    }
                });
            }
        }, 1000);
    }

    public void smsLogin(){
        mLoadingView.showLoading();
        loginService.smsLogin(mLoginView.getPhoneNumber(),mLoginView.getCode(), LoginPresenter.this);
    }


    @Override
    public void onSuccess(final String flag, final JSONObject jsonObject) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mLoginView.toResult(flag,jsonObject);
                mLoadingView.hideLoading();
            }
        });
    }

    @Override
    public void onFail(final String flag, final int code, final String msg) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mLoginView.showError(flag, code, msg);
                mLoadingView.hideLoading();
            }
        });
    }
}
